package com.noble.meether.chat.ui.imui.base;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;

import com.noble.glamour.module.logic.config.Constant;
import com.noble.imui.chatinput.ChatInputView;
import com.noble.imui.messagelist.commons.ImageLoader;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.log.PSP;
import com.noble.library.utils.ScreenUtils;
import com.noble.library.utils.UIUtil;
import com.noble.meether.R;
import com.noble.meether.chat.ui.imui.view.ChatView;

/**
 * 私聊页
 */
public class IMBaseActivity extends BaseAppCompatActivity implements View.OnTouchListener {
    protected ChatView mChatView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setCanNotify(false);//设置该页面不弹出悬浮窗消息通知
        super.onCreate(savedInstanceState);
        setContentView(R.layout.im_chat_act);
        initView();
        initTouchEvent();
    }

    private void initView() {
        PSP.getInstance().put(Constant.PRIVATE_CHAT_TOP_H, UIUtil.getViewHeight(getTitleView()) + ScreenUtils.getStatusBarHeight(this));
        mChatView = findViewById(R.id.chat_view);
        mChatView.initModule();
        mChatView.getChatInputView().getInputView().setOnTouchListener((view, motionEvent) -> {
            scrollToBottom();
            return false;
        });
    }

    protected ImageLoader getImageLoader() {
        return new ImageLoader() {
            @Override
            public void loadAvatarImage(ImageView avatarImageView, String string) {
                com.noble.library.image.ImageLoader.loadCircleAvatar(getApplicationContext(), string, avatarImageView);
            }

            // 图片消息
            @Override
            public void loadImage(final ImageView imageView, String string) {
                com.noble.library.image.ImageLoader.loadRoundCenterCrop(getApplicationContext(), string, imageView);
            }

            // 视频消息
            @Override
            public void loadVideo(ImageView imageCover, String thumbPic, String uri) {
                com.noble.library.image.ImageLoader.loadRoundAvatar(getApplicationContext(), thumbPic, imageCover);
            }
        };
    }

    // ======================================= chatView触摸事件 =======================================
    private InputMethodManager mImm;
    private Window mWindow;

    private void initTouchEvent() {
        this.mImm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mWindow = getWindow();
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                ChatInputView chatInputView = mChatView.getChatInputView();
                if (chatInputView.getMenuState() == View.VISIBLE) {
                    chatInputView.dismissMenuLayout();
                }
                try {
                    View v = getCurrentFocus();
                    if (mImm != null && v != null) {
                        mImm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                        mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                        view.clearFocus();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case MotionEvent.ACTION_UP:
                view.performClick();
                break;
        }
        return false;
    }

    private void scrollToBottom() {
        new Handler().postDelayed(() -> mChatView.getMessageListView().smoothScrollToPosition(0), 200);
    }
}
